package com.blb.ssh.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;

import com.blb.ssh.dao.UserDao;
import com.blb.ssh.entity.Menu;
import com.blb.ssh.entity.Role;
import com.blb.ssh.entity.User;

public class UserDaoImpl implements UserDao {
	
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}


	@SuppressWarnings("unchecked")
	public User getUserByUserName(String username){
		Session session =sessionFactory.getCurrentSession();
		
		Query<User> query = session.createQuery("from User where username=:username");
		query.setParameter("username", username);
		List<User> list = query.list();

		if(list.isEmpty()){
			return null;
		}
		
		return list.get(0);
	}
	
	@SuppressWarnings("unchecked")
	public Long getUserCount(){
		Session session =sessionFactory.getCurrentSession();
		
		String hql="select count(*) from User";
		Query<Long> query = session.createQuery(hql);
		@SuppressWarnings("deprecation")
		long count=query.iterate().next().intValue();
		
	
		return count;
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getUserListByMap(int page, int rows){
		Session session =sessionFactory.getCurrentSession();
		
		
		List<User> ulist=new ArrayList<User>();
		Query<User> query = session.createQuery("from User");
		query.setFirstResult((page-1)*rows);
		query.setMaxResults(rows);
		List<User> list = query.list();
		for(User user:list){
			ulist.add(user);
		}
		
		return ulist;
	}
	
	public boolean batchDeleteUser(String[] ids){
		Session session =sessionFactory.getCurrentSession();
		
		
		boolean isok=true;
		String hql="";
		for(int i=0;i<ids.length;i++){
			if(i ==0){
				hql="id='"+ids[i]+"'";
			}else{
				hql=hql+" or id='"+ids[i]+"'";
			}
		}
		Query query = session.createQuery("delete from User where "+hql);
		query.executeUpdate();
		
		
		return isok;
	}
	
	public boolean addUser(User user){
		Session session =sessionFactory.getCurrentSession();
	
		
		boolean isok=true;
		User u=new User();
		u.setUsername(user.getUsername());
		u.setPassword(user.getPassword());
		u.setRoleid(user.getRoleid());
		session.save(u);
		
		
		return isok;
	}
	
	public boolean editUser(User user){
		Session session =sessionFactory.getCurrentSession();
		
		
		boolean isok=true;
		User u=session.get(User.class,user.getId());
		u.setUsername(user.getUsername());
		u.setPassword(user.getPassword());
		u.setRoleid(user.getRoleid());
		session.update(u);
		
	
		return isok;
	}
	
	@SuppressWarnings("unchecked")
	public List<Role> getAllRoleList(){
		Session session =sessionFactory.getCurrentSession();
		
		List<Role> rlist=new ArrayList<Role>();
		Query<Role> query = session.createQuery(" from Role ");
		List<Role> list = query.list();
		for(Role role:list){
			rlist.add(role);
			//System.out.println(role.getRoleId()+role.getRoleName());
		}
		
		return rlist;
	}
	
	public List<Menu> buttonControl(int roleId,int menui){
		Session session =sessionFactory.getCurrentSession();
		
		
		List<Menu> mlist=new ArrayList<Menu>();
		NativeQuery<Menu> nativeQuery = session.createSQLQuery("select t1.*  from t_role_menu t,t_menu t1 where t.menu_id=t1.menu_id and menu_pid=:menuID and role_id=:roleId");
		nativeQuery.setParameter("roleId", roleId);
		nativeQuery.setParameter("menuID", menui);
//		nativeQuery.executeUpdate();
		for(Menu menu:nativeQuery.addEntity(Menu.class).list()){
			Menu m=new Menu();
			m.setMenuId(menu.getMenuId());
			m.setMenuName(menu.getMenuName());
			m.setMenuPid(menu.getMenuPid());
			m.setMenuUrl(menu.getMenuUrl());
			m.setMenuRole(menu.getMenuRole());
			m.setMenuType(menu.getMenuType());
			mlist.add(m);
		}
		return mlist;
	}
}
